package com.yanqu.road.server.cmd.orewar.task;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.orewar.UserOrePit;
import com.yanqu.road.entity.task.args.OreWarActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.BaseTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.OreWarModule;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class GetRewardTask extends BaseTask {

    private int activityId;

    private long getRewardTime;

    private Property occupyReward;

    public int getActivityId() {
        return activityId;
    }

    public GetRewardTask(YanQuMessage packet, GamePlayer player) {
        super(packet, player);
    }

    @Override
    protected boolean parseData() throws Exception {
        OreWarProto.OreGetRewardReqMsg reqMsg = OreWarProto.OreGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    protected void execute() {
        getRewardTime = System.currentTimeMillis() / 1000;
        occupyReward = new Property(); 
        List<UserOrePit> userPitList = OreWarMgr.getUserOrePitList(activityId,player.getUserId());
        for(UserOrePit userOrePit : userPitList){
            long lastGetRewardTime = userOrePit.getLastGetRewardTime();
            Property reward = OreWarMgr.getPitOccupyReward(activityId,player.getUserId(), userOrePit, getRewardTime, true);
            occupyReward.addProperty(reward);
            LogMgr.addLogOreWarReward(player.getUserId(), userOrePit.getActivityId(), userOrePit.getPitId(), PropertyHelper.parsePropertyToString(occupyReward),
                    getRewardTime - lastGetRewardTime, false, eLogMoneyType.OreWarGetOccupyReward.getValue());
        }
        //算广场的收益
        Property reward = player.getModule(OreWarModule.class).getOccupyReward(activityId,getRewardTime, 0);
        occupyReward.addProperty(reward);
        //收益入袋
        player.getModule(CurrencyModule.class).addCurrency(occupyReward, eLogMoneyType.OreWar, eLogMoneyType.OreWarGetOccupyReward);
        if(!StringUtils.isNullOrEmpty(PropertyHelper.parsePropertyToString(occupyReward))){//有领到收益
            //增加一次领取收益
            player.notifyListener(eGamePlayerEventType.MineWarGetRewardTimes.getValue(), 1);
            OreWarActivityMgr.conditionNotify(player.getUserId(),eGamePlayerEventType.OreWarAcCoinOutPut.getValue(),
                    new OreWarActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, occupyReward.getCountByGoodsId(GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID),
                            player.getUserInfo().getUnionUid(), activityId));
        }
    }

    @Override
    protected void sendData(GamePlayer player) {
        OreWarProto.OreGetRewardRespMsg.Builder respMsg = OreWarProto.OreGetRewardRespMsg.newBuilder();
        if(ret == 0){
            respMsg.setLastGetTime(getRewardTime);
            respMsg.setOutputCount1(occupyReward.getCountByGoodsId(GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID).longValue());
        }
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_ORE_WAR_GET_REWARD, respMsg);

    }
}
